uniform sampler3D volume;

void main( )
{
// 
// Define the sample postion and offsets for the derivatives.
//
   //vec3 pos = vec3(gl_TexCoord[0]);
   vec4 scaleValue = texture3D( volume, vec3(gl_TexCoord[0]) );
//
// If the value is insignificant, kill the fragment
//
   if( scaleValue.a >= 0.08123 )
   {
      const vec3 delX = vec3(0.005, 0.0, 0.0);
      const vec3 delY = vec3(0.0, 0.005, 0.0);
      const vec3 delZ = vec3(0.0, 0.0, 0.005);
      //
      // Sample the texture
      //
      vec4 cx = texture3D( volume, vec3(gl_TexCoord[0])+delX );
      vec4 cy = texture3D( volume, vec3(gl_TexCoord[0])+delY );
      vec4 cz = texture3D( volume, vec3(gl_TexCoord[0])+delZ );
      //
      // For central differences, we need three more samples.
      //
      //vec4 cmx = texture3D( volume, vec3(gl_TexCoord[0])-delX );
      //vec4 cmy = texture3D( volume, vec3(gl_TexCoord[0])-delY );
      //vec4 cmz = texture3D( volume, vec3(gl_TexCoord[0])-delZ );
      //
      // Calculate the gradient vector at the sample position.
      // 	
      vec3 gradient;
      gradient.x = cx.a-scaleValue.a;
      gradient.y = cy.a-scaleValue.a;
      gradient.z = cz.a-scaleValue.a;
      gradient = normalize( gradient );
      //
      // Set-up a test light. Should be replaced with a Uniform variable.
      //
      vec3 lightdir = vec3( -0.57735, -0.57735, 0.57735 );
      //lightdir = normalize( lightdir );
      //
      // Calculate a diffuse lighting. This is a weird shading, in that
      //   negative dot products take away ambient light.
      //
      float val = 0.3 + 0.4*dot( gradient, lightdir );
      vec3 lightdir2 = vec3( -0.7071, 0.7071, 0.0 );
      val = max( 0.0, val );
      val += 0.5*max( dot( gradient, lightdir2 ), 0.0 );
      val = min( 1.0, val );
      //
      // Modulate the object color by the lighting.
      //
      gl_FragColor = val * gl_Color;
      //
      //Set the opacity using the original texture sample and the input opacity.
      //
      gl_FragColor.a = gl_Color.a * min(1.0, 0.1+scaleValue.a);
   }
   else
      gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
} 